<template>
  <div
    style="width: 60%; margin-left: 20%; margin-right: 20%; margin-top: 20px"
  >
    <el-form label-position="right" label-width="80px" :model="work">
      <el-form-item label="用户">
        <el-input disabled v-model="work.user.username"></el-input>
      </el-form-item>
      <el-form-item label="帖子标题">
        <el-input disabled v-model="work.header"></el-input>
      </el-form-item>
      <el-form-item label="帖子内容">
        <el-button v-if="!isAll" @click="isAll = true">展开</el-button>
        <el-button v-else @click="isAll = false">收起</el-button>
        <el-card>
          <p v-if="isAll" v-html="work.content"></p>
          <p v-else class="text-clamp" v-html="work.content"></p>
        </el-card>
      </el-form-item>
      <el-form-item label="审批理由">
        <el-input
          :disabled="work.isPass === 0 ? false : true"
          v-model="work.reason"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-radio
          :disabled="work.isPass !== 0 ? true : false"
          v-model="pass"
          :label="1"
          >通过</el-radio
        >
        <el-radio
          :disabled="work.isPass !== 0 ? true : false"
          v-model="pass"
          :label="2"
          >不通过</el-radio
        >
      </el-form-item>
      <el-form-item v-if="work.isPass === 0" label="提交审核">
        <el-button type="" @click="examine">审核</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import work from "@/api/work";

export default {
  data() {
    return {
      work: {},
      size: "",
      isAll: false,
      disabled: false,
      pass: "",
    };
  },

  created() {
    this.init();
  },

  methods: {
    init() {
      this.work = JSON.parse(this.$route.query.work);
      this.pass = this.work.isPass;
    },
    examine() {
      this.$confirm("此操作将提交审核，不可修改, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.work.isPass = this.pass;
          if (this.work.isPass === 1) {
            work.pass(this.work.workId, this.work.reason).then((res) => {
              if (res.success) {
                this.$message({
                  message: "提交成功",
                  type: "success",
                });
              } else {
                this.$message({
                  message: "审核失败，请稍后再试",
                  type: "error",
                });
              }
            });
          } else {
            work.noPass(this.work.workId, this.work.reason).then((res) => {
              if (res.success) {
                this.$message({
                  message: "提交成功",
                  type: "success",
                });
              } else {
                this.$message({
                  message: "审核失败，请稍后再试",
                  type: "error",
                });
              }
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消审核",
          });
        });
    },
  },
};
</script>

<style scope>
.text-clamp {
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4;
}
</style>